import { $each } from '../index';
import { handleConditionExtend } from '../conditionFormat/useConditionFormat.js';
export function funnelFormat (datasets, { numberLabelConversionRate }) {
  const { dimensions, type, source, fieldNameChainMap, conditionData, conditionOptions } = datasets;
  const { yAxis, legend: legendKey, xAxis } = dimensions;
  let seriesData = [];
  if (!yAxis) {
    return {seriesData: []};
  }
  if (type === 'STATIC') {
      seriesData.push({
        ...formatData({
          data: source,
          numberLabelConversionRate
        }),
      });
  } else {
    const data = [];
    $each(source, (item,index)=>{
      const itemData =  {
        name: item[xAxis],
        value: item[yAxis],
        values: [item[xAxis]],
      }
      data.push(itemData);
    })
    if(legendKey) {
      const legendMap = source.reduce((acc, current, sourceIndex) => {
        const itemValue = {
          name: current[xAxis],
          value: current[yAxis],
          values: [current[xAxis]],
        }
          // 根据原始字段名称收集数据, 去重
        if (fieldNameChainMap && fieldNameChainMap?.legend[legendKey]) {
          !conditionData[fieldNameChainMap?.legend[legendKey]].filter(legend=> legend.value === current[legendKey]).length  &&  conditionData[fieldNameChainMap?.legend[legendKey]].push({ value: current[legendKey] });
        }
        if (acc[current[legendKey]]) {
          acc[current[legendKey]].data.push(itemValue)
        } else {
          acc[current[legendKey]] =
            {
              name: current[legendKey],
              data: [itemValue],
            }
        }
        return acc;
      }, {});
      $each(legendMap, (item,itemKey)=>{
        seriesData.push({
          ...formatData({
            data: item.data,
            numberLabelConversionRate,
            fieldNameChainMap,
            conditionData,
            xAxis,
            yAxis,
            legend: legendKey
          }),
          name: itemKey,
          dataFormatType: 'yAxis'
        });
      })
    } else {
      seriesData.push({
        ...formatData({
          data,
          numberLabelConversionRate,
          fieldNameChainMap,
          conditionData,
          xAxis,
          yAxis
        }),
        name: yAxis,
      });
    }
  }
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: legendKey ? true : false, // 是否存在颜色维度
    seriesData,
    legendFieldName: fieldNameChainMap['legend']?.[legendKey],
    fieldNameChainMap,
  };
  handleConditionExtend(options, conditionOptions);
  return {
    seriesData: seriesData
  }
}
function sortByValue(source) {
  return source.sort((a, b)=>{
    let x = a['value'];
    let y = b['value'];
    return x > y ? -1 : x < y ? 1 : 0;
  })
}
function formatData({data, numberLabelConversionRate, fieldNameChainMap='', conditionData= '',xAxis='', yAxis='', legend=''}) {
  const sortData = sortByValue(data);
  // 根据原始字段名称收集数据
  $each(sortData, (itemData, index)=>{
    const { name , value } = itemData ;
    itemData.dataFormatType = 'yAxis';
           // 根据原始字段名称收集数据
    if (fieldNameChainMap && fieldNameChainMap?.xAxis[xAxis] && xAxis  ) {
        conditionData[fieldNameChainMap?.xAxis[xAxis]].push({ value: name });
    }
    if (fieldNameChainMap && fieldNameChainMap?.yAxis[yAxis] && yAxis && fieldNameChainMap ) {
      conditionData[fieldNameChainMap?.yAxis[yAxis]].push({ value: value });
    }
    if(index===0) {
      itemData['changeValue'] = '100%';
    } else {
      const value = numberLabelConversionRate.value ==='upperStory' ? sortData[index-1]?.value : sortData[0]?.value
      const itemValue = itemData.value/value * 100;
      itemData['changeValue'] = `${ itemValue.toString().includes('.') ? Number(itemValue.toFixed(2)) : itemValue }%`
    }
  })
  return { data: sortData };
}
